package org.csu.pet_store_2.Controller;

import org.csu.pet_store_2.Common.CommonResponse;
import org.csu.pet_store_2.Domain.Order;
import org.csu.pet_store_2.Domain.OrderVO;
import org.csu.pet_store_2.Service.CartService;
import org.csu.pet_store_2.Service.LineItemService;
import org.csu.pet_store_2.Service.OrderService;
import org.csu.pet_store_2.persistence.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Transactional
public class OrderController {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private LineItemService lineItemService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private CartService cartService;

    /*
    获取一个用户的所有订单信息
    http://localhost:8080/getorders?id=ACID2
   {
    "status": 0,
    "msg": "SUCCESS",
    "data": [
        {
            "orderid": 1011,
            "userid": "ACID2",
            "orderDate": "2022-11-12T16:00:00.000+00:00",
            "shipAddr1": "582",
            "shipAddr2": "825",
            "shipCity": "4214",
            "shipState": "ok",
            "shipZip": "41",
            "shipCountry": "14",
            "billAddr1": "582",
            "billAddr2": "825",
            "billCity": "4214",
            "billState": "ok",
            "billZip": "41",
            "billCountry": "14",
            "courier": "UPS",
            "totalPrice": 18.50,
            "billToFirstname": "muffin",
            "billToLastname": "muffin",
            "shipToFirstname": "muffin",
            "shipToLastname": "muffin",
            "creditCard": "999 9999 9999 9999",
            "exprDate": "12/2022",
            "cardType": "Visa",
            "locale": "default",
            "lineItemList": [
                {
                    "orderid": 1011,
                    "linenum": 0,
                    "itemid": "EST-4",
                    "quantity": 1,
                    "unitprice": "18.50"
                }
            ]
        },
        {
            "orderid": 1012,
            "userid": "ACID2",
            "orderDate": "2022-11-12T16:00:00.000+00:00",
            "shipAddr1": "582",
            "shipAddr2": "825",
            "shipCity": "4214",
            "shipState": "ok",
            "shipZip": "41",
            "shipCountry": "14",
            "billAddr1": "582",
            "billAddr2": "825",
            "billCity": "4214",
            "billState": "ok",
            "billZip": "41",
            "billCountry": "14",
            "courier": "UPS",
            "totalPrice": 18.50,
            "billToFirstname": "muffin",
            "billToLastname": "muffin",
            "shipToFirstname": "muffin",
            "shipToLastname": "muffin",
            "creditCard": "999 9999 9999 9999",
            "exprDate": "12/2022",
            "cardType": "Visa",
            "locale": "default",
            "lineItemList": [
                {
                    "orderid": 1012,
                    "linenum": 0,
                    "itemid": "EST-4",
                    "quantity": 1,
                    "unitprice": "18.50"
                }
            ]
        }
    ]
}
     */
    @GetMapping("/getorders")
    public CommonResponse<List<Order>> getOrderByUserid(@RequestParam("id")String userId) {
        List<Order> orderList = orderMapper.getOrdersListByUserid(userId);
        for (int i=0;i<orderList.size();i++)
        {
            orderList.get(i).setLineItemList(lineItemService.getLineItemByorderId(orderList.get(i).getOrderid().toString()));
        }
        return CommonResponse.creatForSuccess(orderList);
    }
    /*
    创建新订单,写入数据库,并写入lineitem
    创建订单的同时，应该清空购物车
    post方法
    {
            "userid": "ACID2",
            "orderDate": "2022-11-12T16:00:00.000+00:00",
            "shipAddr1": "582",
            "shipAddr2": "825",
            "shipCity": "4214",
            "shipState": "ok",
            "shipZip": "41",
            "shipCountry": "14",
            "billAddr1": "582",
            "billAddr2": "825",
            "billCity": "4214",
            "billState": "ok",
            "billZip": "41",
            "billCountry": "14",
            "courier": "UPS",
            "totalPrice": 18.50,
            "billToFirstname": "muffin",
            "billToLastname": "muffin",
            "shipToFirstname": "muffin",
            "shipToLastname": "muffin",
            "creditCard": "999 9999 9999 9999",
            "exprDate": "12/2022",
            "cardType": "Visa",
            "locale": "default",
            "lineItemList": [
                {

                    "linenum": 0,
                    "itemid": "EST-4",
                    "quantity": 1,
                    "unitprice": "18.50"
                }
            ]
        }
     */
    @PostMapping("/addorder")
    public CommonResponse<String> addorder(@RequestBody Order order){
        orderMapper.insert(order);
        for(int i=0;i<order.getLineItemList().size();i++){
            order.getLineItemList().get(i).setOrderid(order.getOrderid());
            order.getLineItemList().get(i).setLinenum(i);
        }
        lineItemService.addnewlineitem(order.getLineItemList());
        //cartService.cleancart(order.getUserid());这一句用来情空购物车，但是还没匹配好。
        return CommonResponse.creatForSuccess("添加成功");

    }

    @GetMapping("/getvalidorderid")
    public CommonResponse<String> getvalidorderid()
    {
        Integer mm=orderService.createValidOrderId()+1;
        return CommonResponse.creatForSuccess(mm.toString());
    }

    /*
    获取单个的order
    http://localhost:8080/getorder?orderid=1007
     */
    @GetMapping("/getorder")
    public CommonResponse<Order> getOrderByOrderId(@RequestParam("orderid") String orderid)
    {
        Order order=orderMapper.getOrderByOrderid(Integer.parseInt(orderid));

        order.setLineItemList(lineItemService.getLineItemByorderId(order.getOrderid().toString()));

        return CommonResponse.creatForSuccess(order);
    }
}

